﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Test
{
    public partial class UpdateUserInfo : System.Web.UI.Page
    {
        class TesterUserInfo
        {
            public string level { get; set; }
            public string nickname { get; set; }
            public int user_id { get; set; }
            public string unionid { get; set; }
            public string openid_biz { get; set; }
            public int game_id_bozhong { get; set; }
            public int parent1_uid { get; set; }
            public int parent2_uid { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // { "subscribe": 1, "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", "nickname": "Band", "sex": 1, "language": "zh_CN", "city": "广州", "province": "广东", "country": "中国", "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", "subscribe_time": 1382694957, "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL" "remark": "", "groupid": 0, "tagid_list": [128, 2]}
            string action = Request.QueryString["action"];
            if(!string.IsNullOrEmpty(action) && action == "onekey")
            {
                IList<TesterUserInfo> users = new List<TesterUserInfo>();
                users.Add(new TesterUserInfo() { level = "1级", nickname = "詹辉", user_id = 1000, unionid = "ogQPs1S_uyyovmOcreV-R86BWsFI", openid_biz = "ou7IyxPpAeLI0RWr7STkTudcqAPs", game_id_bozhong = 0, parent1_uid = 0, parent2_uid = 0 });
                users.Add(new TesterUserInfo() { level = "2级", nickname = "兔子哥哥", user_id = 1001, unionid = "ogQPs1Xkv43KcKJk9SEGTN0XwdkQ", openid_biz = "ou7IyxEhRDaL6JRpNYrnkPsOcSlw", game_id_bozhong = 0, parent1_uid = 1000, parent2_uid = 0 });
                users.Add(new TesterUserInfo() { level = "3级", nickname = "Li.Li.Li", user_id = 1002, unionid = "ogQPs1YerRbkU8hPTx5hnld18i1w", openid_biz = "ou7IyxFMCvAN8_FVnAJn6W3nT2r0", game_id_bozhong = 0, parent1_uid = 1001, parent2_uid = 1000 });
                users.Add(new TesterUserInfo() { level = "4级", nickname = "苏苏的苏", user_id = 1003, unionid = "ogQPs1c0RVmMfmBHZPYEIIC7r4qc", openid_biz = "ou7IyxNMteGQIQjHuRN5ehuNvGYY", game_id_bozhong = 0, parent1_uid = 1002, parent2_uid = 1001 });
                users.Add(new TesterUserInfo() { level = "5级", nickname = "天澜", user_id = 1004, unionid = "ogQPs1X6bAtttToMgBcUxHNLlRzc", openid_biz = "ou7IyxIe-0-lU4uqV17aDI7EOW08", game_id_bozhong = 0, parent1_uid = 1003, parent2_uid = 1002 });
                users.Add(new TesterUserInfo() { level = "6级", nickname = "丘", user_id = 1005, unionid = "ogQPs1cMeidWbOF_--ecvYR6Kz-M", openid_biz = "ou7IyxEDJV_AqxsKTisdWJiBrzmQ", game_id_bozhong = 0, parent1_uid = 1004, parent2_uid = 1003 });

                string errmsg, infocontent;
                foreach(TesterUserInfo user in users)
                {
                    infocontent = "{'subscribe':1,'openid':'" + user.openid_biz + "','nickname':'" + user.nickname + "','sex':1,'language':'zh_CN','city':'厦门','province':'福建','country':'中国','headimgurl':'http:\\/\\/wx.qlogo.cn\\/mmopen\\/Vtehib1G1vKjuF8BtLvAE0uicrvXCmwjXujhbBV5Phql0TMQMAIu01xadVekpfIzRJGoJVhQHSroW7xJre3UI8rhgyjTAb7QRR\\/0','subscribe_time':1493258579,'unionid':'" + user.unionid + "','remark':'','groupid':0,'tagid_list':[]}";
                    WxUserInfoResult wxUser = JSON.parse<WxUserInfoResult>(infocontent.Replace("'", "\""));
                    wxUser.game_id_bozhong = UserModels.GetGameID(0, wxUser.unionid, "bozhong", out errmsg);
                    Response.Write(errmsg);
                    wxUser.user_id = UserModels.WxLoginCallbackUpdateDb(wxUser,0);
                    string sqlstmt = string.Format("select parent1_uid, parent2_uid from t_user where user_id=" + wxUser.user_id);
                    System.Data.DataTable dtUserTemp = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
                    if (null != dtUserTemp || dtUserTemp.Rows.Count > 0)
                    {
                        wxUser.parent1_uid = StringUtil.ToInteger(dtUserTemp.Rows[0]["parent1_uid"].ToString());
                        wxUser.parent2_uid = StringUtil.ToInteger(dtUserTemp.Rows[0]["parent2_uid"].ToString());
                    }
                }
                Response.Redirect("Default.aspx?success=1");
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string nickname = tb_nickname.Text;
            string unionid = tb_unionid.Text;
            string openid = tb_openid.Text;
            string infocontent = "{'subscribe':1,'openid':'"+openid+"','nickname':'"+ nickname + "','sex':1,'language':'zh_CN','city':'厦门','province':'福建','country':'中国','headimgurl':'http:\\/\\/wx.qlogo.cn\\/mmopen\\/Vtehib1G1vKjuF8BtLvAE0uicrvXCmwjXujhbBV5Phql0TMQMAIu01xadVekpfIzRJGoJVhQHSroW7xJre3UI8rhgyjTAb7QRR\\/0','subscribe_time':1493258579,'unionid':'"+ unionid + "','remark':'','groupid':0,'tagid_list':[]}";
            WxUserInfoResult wxUser = JSON.parse<WxUserInfoResult>(infocontent.Replace("'","\""));
            if (null == wxUser)
            {
                Response.Write("wxUser 解析失败");
                Response.End();
            }
            // 更新用户表字段
            try
            {
                string errmsg;
                wxUser.game_id_bozhong = UserModels.GetGameID(0, wxUser.unionid, "bozhong", out errmsg);
                Response.Write(errmsg);
                wxUser.user_id = UserModels.WxLoginCallbackUpdateDb(wxUser,0);
                string sqlstmt = string.Format("select parent1_uid, parent2_uid from t_user where user_id=" + wxUser.user_id);
                System.Data.DataTable dtUserTemp = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
                if (null != dtUserTemp || dtUserTemp.Rows.Count > 0)
                {
                    wxUser.parent1_uid = StringUtil.ToInteger(dtUserTemp.Rows[0]["parent1_uid"].ToString());
                    wxUser.parent2_uid = StringUtil.ToInteger(dtUserTemp.Rows[0]["parent2_uid"].ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("[LoginCallBack-GetInfo] 获取user_id和game_id出错："+ex.Message);
            }

            Session.Add("login2", wxUser); // 保存会话消息

            // 记录日志
            Response.Write(string.Format("[LoginCallBack] 登录成功 uid={0},game_id_bozhong={1}", wxUser.user_id, wxUser.game_id_bozhong));
        }
    }
}